package test.helpers;

import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedAction;

import sun.misc.Unsafe;

import com.google.common.base.Throwables;
import static com.google.common.base.Preconditions.*;

/**
 * @author cheremin
 * @since 19.04.12,  20:55
 */
public class UnsafeHelper {
    private static final Unsafe unsafe;


    static {
        unsafe = AccessController.doPrivileged(
                new PrivilegedAction<Unsafe>() {
                    @Override
                    public Unsafe run() {
                        try {
                            final Field f = Unsafe.class.getDeclaredField( "theUnsafe" );
                            f.setAccessible( true );
                            return ( Unsafe ) f.get( null );
                        } catch ( Exception e ) {
                            throw Throwables.propagate( e );
                        }
                    }
                } );
    }

    public static Unsafe unsafe() {
        return unsafe;
    }
}